﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AirForceCDCTests.src.main;
using System.ComponentModel;

namespace AirForceCDCTests
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static DebugWindow DebugWindow = new DebugWindow();
        public static ErrorWindow ErrorWindow = new ErrorWindow();

        private List<Question> questions;
        private List<Question> wrongQuestions;
        private List<Question> rightQuestions;
        private int currentQuestionNumber = 0;
        private bool finished = false;

        public MainWindow()
        {
            
#if DEBUG
            DebugWindow.Show();
#endif
            DebugWindow.AddMessage("Starting Air Force CDC Practice Test...");
            DebugWindow.AddMessage("Loading Question Bank");

            InitializeComponent();
            this.Hide();

            QuestionBank qb = new QuestionBank(this);
            qb.Show();
        }

        public void Reload()
        {
            finished = false;
            currentQuestionNumber = 0;
            DebugWindow.ClearWindow();
            QuestionBank qb = new QuestionBank(this);
            qb.Show();
        }


        internal void StartQuestions(List<Question> allQuestions, int numOfQuestions)
        {
            numOfQuestions = numOfQuestions > allQuestions.Count ? allQuestions.Count : numOfQuestions;
            questions = new List<Question>();
            wrongQuestions = new List<Question>();
            rightQuestions = new List<Question>();

            allQuestions.Sort(delegate(Question q1, Question q2)
            {
                return q1.ID.CompareTo(q2.ID);
            }
            );


            for (int i = 0; i < numOfQuestions; i++)
                questions.Add(allQuestions[i]);

            DisplayQuestions();
            
            this.Show();
        }

        private void DisplayQuestions()
        {
            if ((currentQuestionNumber + 1) > questions.Count)
            {
                DisplaySummary();
                return;
            }

           
            current.Content = currentQuestionNumber + 1;
            max.Content = questions.Count;
            answerBox.Text = "";

            Question currentQuestion = questions[currentQuestionNumber];

            questionBox.Text    = currentQuestion.MainQuestion;
            choice1.Text        = currentQuestion.Choices[0];
            choice2.Text        = currentQuestion.Choices[1];
            choice3.Text        = currentQuestion.Choices[2];
            choice4.Text        = currentQuestion.Choices[3];

            if (ErrorWindow.NoError)
                answerBox.Focus();
        }

        private void DisplaySummary()
        {
            int wrongAnswers = wrongQuestions.Count;
            int rightAnswers = rightQuestions.Count;

            float percent = ((float)rightAnswers / (float)questions.Count) * 100f;

            EndWindow window = new EndWindow(this);

            window.SetMessage(String.Format("Total Questions:   {0}\n\n\tCorrect Answers:" +
                            "{1}\n\tWrong Answers: {2}\n\n\tPercent:   {3}%",
                            wrongAnswers + rightAnswers, rightAnswers, wrongAnswers, percent));
            window.Show();

            finished = true;
        }

        internal void ViewMissedQuestions()
        {
            if (wrongQuestions.Count == 0)
            {
                ErrorWindow.SetErrorMessage("There are no missed questions");
                return;
            }
            finished = false;

            questions = wrongQuestions.ToList();
            wrongQuestions.Clear();
            rightQuestions.Clear();

            currentQuestionNumber = 0;

            DisplayQuestions();
        }

        public List<Question> getReviewQuestions()
        {
            return wrongQuestions;
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            ErrorWindow.Close();
            DebugWindow.Close();
            base.OnClosing(e);
        }

        private void On_Submit(object sender, RoutedEventArgs e)
        {
            if (finished) return;
            try
            {

                int answerVal = int.Parse(answerBox.Text);
                PerformCheck(answerVal);
            }
            catch (FormatException)
            {
                int answer;
                switch (answerBox.Text.Trim().ToLower())
                {
                    case "a":
                        answer = 1;
                        break;
                    case "b":
                        answer = 2;
                        break;
                    case "c":
                        answer = 3;
                        break;
                    case "d":
                        answer = 4;
                        break;
                    default:
                        answer = 0;
                        break;
                }
                PerformCheck(answer);
            }
        }

        private void PerformCheck(int answerVal)
        {
            Question currentQuestion = questions[currentQuestionNumber];
            currentQuestion.Selected_Answer = answerVal;
            if (answerVal < 1 || answerVal > 4)
            {
                ErrorWindow.SetErrorMessage("Must Enter 1-4 or A-D");
                DebugWindow.AddMessage("Need to enter 1-4 or A-D");
                DisplayQuestions();
            }

            if (answerVal == currentQuestion.Answer)
            {
                rightQuestions.Add(currentQuestion);
                DebugWindow.AddMessage("--->Correct Answer<---");
            }
            else
            {
                wrongQuestions.Add(currentQuestion);
                DebugWindow.AddMessage("xxx>Wrong Answer<xxx");
            }

            currentQuestionNumber++;

            DisplayQuestions();
        }

        private void Previous_Click(object sender, RoutedEventArgs e)
        {
            if (finished) return;

            currentQuestionNumber = currentQuestionNumber - 1 < 0 ? 0 : currentQuestionNumber - 1;

            Question prevQuestion = questions[currentQuestionNumber];
            if (wrongQuestions.Contains(prevQuestion))
                wrongQuestions.Remove(prevQuestion);
            else if (rightQuestions.Contains(prevQuestion))
                rightQuestions.Remove(prevQuestion);

            DisplayQuestions();
        }

        private void Next_Click(object sender, RoutedEventArgs e)
        {
            if (finished) return;

            wrongQuestions.Add(questions[currentQuestionNumber]);
            currentQuestionNumber++;

            DisplayQuestions();
        }


        private void Close_Click(object sender, RoutedEventArgs e)
        {
            DebugWindow.Close();
            ErrorWindow.Close();
            this.Close();
            base.Close();
        }

        private void answerBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
                On_Submit(sender, e);
            else if (e.Key == Key.F2)
                Previous_Click(sender, e);
            else if (e.Key == Key.F3)
                Next_Click(sender, e);
            else if (e.Key == Key.F1)
            {
                ErrorWindow.Title = "Help";
                ErrorWindow.SetErrorMessage("Enter your answer between 1-4\nPress [Enter] key or SUBMIT button to submit your answer."
                    + "\n\nF2:\tPrevious Question\nF3:\tNext Question");
            }
        }

    }
}
